package com.chuangsi.common.utils;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.chuangsi.common.exception.ServiceException;
import org.springframework.beans.BeanWrapper;
import org.springframework.beans.BeanWrapperImpl;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.LocalDate;
import java.time.Month;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Tool {

    public static final Pattern PASSWORD = Pattern.compile("^(?=.*\\d)(?=.*[a-zA-Z])(?=.*[^\\da-zA-Z\\s]).{8,30}$");
    public static final Pattern localDate_pattern1 = Pattern.compile("\\d{4}\\-\\d{2}\\-\\d{2}");
    public static final Pattern localDate_pattern2 = Pattern.compile("\\d{4}\\-\\d{1,2}\\-\\d{1,2}");

    public static final DateTimeFormatter DATE_TIME_FORMATTER1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter DATE_TIME_FORMATTER2 = DateTimeFormatter.ofPattern("yyyy-M-d");
    public static final BigDecimal ONEHUNDRED = BigDecimal.valueOf(100);
    public static final BigDecimal ZERO = BigDecimal.valueOf(0.00);


    /**
     * 计算日环比  日环比增长率 = [(今日数值 - 昨日数值) / 昨日数值] × 100%
     * @param today
     * @param lastToday
     * @return
     */
    public static String dod(BigDecimal today, BigDecimal lastToday){
        String dod = "100%";
        if(lastToday.compareTo(BigDecimal.ZERO)>0){
            //计算日环比
            BigDecimal differ = today.subtract(lastToday);
            dod = differ.divide(lastToday,2,BigDecimal.ROUND_UP).multiply(BigDecimal.valueOf(100)) +"%";
        }
        return dod;
    }

    /**
     * 计算百分比
     * @param divisor
     * @param dividend
     * @return
     */
    public static BigDecimal rate(Object divisor, Object dividend){
        Double one;
        Double two;
        try {
            one = Double.parseDouble(divisor.toString());
            two = Double.parseDouble(dividend.toString());
        }catch (Exception e){
            return ZERO;
        }
        if(two.equals(0)||two.equals(0.0)){
            return ZERO;
        }
        return BigDecimal.valueOf(one).divide(BigDecimal.valueOf(two), 4, BigDecimal.ROUND_HALF_UP).multiply(ONEHUNDRED).setScale(2,BigDecimal.ROUND_HALF_UP);
    }

    /**
     * 计算平均值
     * @param total
     * @return
     */
    public static BigDecimal avg(Object total, int num){
        Double one;
        try {
            one = Double.parseDouble(total.toString());
        }catch (Exception e){
            return ZERO;
        }
        if(num==0){
            return ZERO;
        }
        return BigDecimal.valueOf(one).divide(BigDecimal.valueOf(num), 2, BigDecimal.ROUND_HALF_UP);
    }



    /**
     * 星冠牌照颜色
     * @param color
     * @return
     */
    public static String getColorByCode(String color){
        if("1".equals(color)){
            color="蓝色";
        }else if("2".equals(color)){
            color="黄色";
        }else if("3".equals(color)){
            color="黑色";
        }else if("4".equals(color)){
            color="白色";
        }else{
            color="其他";
        }
        return color;
    }

    public static boolean isPassWord(String str) {
        if (StringUtils.isBlank(str)) {
            return false;
        }
        if (PASSWORD.matcher(str).matches()) {
            return true;
        }
        return false;
    }



    /**
     *
     * 根据身份证号码判断男女
     *
     * @param idCard 身份证号
     * @return 性别，1：男，2：女
     */
    public static String getSex(String idCard) {
        String sex="1";
        if(idCard.length()!=18){
            return sex;//默认男
        }
        Integer sexNumber= Integer.parseInt(String.valueOf(idCard.charAt(16)));
        if((sexNumber&2)==0){
            sex="2";
        }
        if(sexNumber==1){
            sex="1";
        }
        return sex;
    }

    /**
     *
     * 根据身份证号码判断男女
     *
     * @param idCard 身份证号
     * @return 性别，1：男，2：女
     */
    public static String getSexName(String idCard) {
        if(StringUtils.isBlank(idCard)){
            return "";
        }
        String sex="男";
        if(idCard.length()!=18){
            return sex;//默认男
        }
        Integer sexNumber;
        try {
            sexNumber= Integer.parseInt(String.valueOf(idCard.charAt(16)));
        }catch (NumberFormatException e){
            return sex;//默认男
        }
        if((sexNumber%2)==0){
            sex="女";
        }else{
            sex="男";
        }
        return sex;
    }



    /**
     * 验证提前30天
     * 当前日期 比 传入的日期 小于 等于30天
     *
     * @param date
     * @return true 满足小于等于30天，false 不满足
     */
    public static boolean verify30(LocalDate date) {
        if (date == null) {
            return false;
        }
        //传入的日期 - 30 天小于等于 当前日期
        return date.minusDays(30).compareTo(LocalDate.now())<=0;
    }



    /**
     * 验证传入的日期过期，
     *
     * 判断传入的日期 小于 当前日期 就是已过期
     * @param date
     * @return
     */
    public static boolean verifyNow(LocalDate date){
        if (date == null) {
            return false;
        }
        //传入的日期小于于当前日期 就是已过期
        return date.compareTo(LocalDate.now()) < 0;
    }

    /**
     * 获取当前日期所在季度的开始日期和结束日期
     * 季度一年四季， 第一季度：1月-3月， 第二季度：4月-6月， 第三季度：7月-9月， 第四季度：10月-12月
     * @param isFirst  true表示查询本季度开始日期  false表示查询本季度结束日期
     * @return
     */
    public static LocalDate getStartOrEndDayOfQuarter(Boolean isFirst){
        LocalDate today=LocalDate.now();
        LocalDate resDate = LocalDate.now();
        if (today == null) {
            today = resDate;
        }
        Month month = today.getMonth();
        Month firstMonthOfQuarter = month.firstMonthOfQuarter();
        Month endMonthOfQuarter = Month.of(firstMonthOfQuarter.getValue() + 2);
        if (isFirst) {
            resDate = LocalDate.of(today.getYear(), firstMonthOfQuarter, 1);
        } else {
            resDate = LocalDate.of(today.getYear(), endMonthOfQuarter, endMonthOfQuarter.length(today.isLeapYear()));
        }
        return resDate;
    }

    public static LocalDate dateToLocalDate(Date date) {
        if(null == date) {
            return null;
        }
        return date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
    }

    public static Boolean isFieldEmpty(String fieldName,Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();
        boolean find = false;
        for (Field field : fields) {
            boolean accessible = field.isAccessible();
            field.setAccessible(true);
            try {
                if (field.getName().equalsIgnoreCase(fieldName)) {
                    find = true;
                    Object o = field.get(obj);
                    if (o instanceof String) {
                        String s = (String) o;
                        if (s.equals("") || s == null) {
                            return true;
                        }
                    } else if (o == null) {
                        return true;
                    }
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            field.setAccessible(accessible);
        }
        if (!find) {
            return null;
        }
        return false;
    }


    public static boolean filterData(Object obj, String filedName, List<String> vals) {
        boolean flag=false;
        for (Field field : obj.getClass().getDeclaredFields()) {
            field.setAccessible(true);
            if(field.getName().toLowerCase().equals(filedName)){
                try{
                    Object val=field.get(obj);
                    if(val!=null&&vals.contains(val)){
                        flag=true;
                    }
                }catch (IllegalAccessException e){
                    return false;
                }
            }
        }
        return flag;
    }

    /**
     * 获取所有字段为null的属性名
     * @param source
     * @return
     */
    public static String[] getNullPropertyNames (Object source) {
        final BeanWrapper src = new BeanWrapperImpl(source);
        java.beans.PropertyDescriptor[] pds = src.getPropertyDescriptors();

        Set<String> emptyNames = new HashSet<String>();
        for(java.beans.PropertyDescriptor pd : pds) {
            Object srcValue = src.getPropertyValue(pd.getName());
            if (srcValue == null) emptyNames.add(pd.getName());
        }
        String[] result = new String[emptyNames.size()];
        return emptyNames.toArray(result);
    }

    /**
     * 获取访问真实IP
     * @return
     */
    public static String getIp() {
        String ipAddress = "";
        try {
            ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
            HttpServletRequest request = attributes.getRequest();
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("X-Real-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
                if (ipAddress.equals("127.0.0.1") || ipAddress.equals("0:0:0:0:0:0:0:1")) {
                    //根据网卡取本机配置的IP
                    InetAddress inet = null;
                    try {
                        inet = InetAddress.getLocalHost();
                    } catch (UnknownHostException e) {
                        e.printStackTrace();
                    }
                    ipAddress = inet.getHostAddress();
                }
            }
            //对于通过多个代理的情况，第一个IP为客户端真实IP,多个IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) { //"***.***.***.***".length() = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        }catch (Exception ex){
            return "";
        }
        return ipAddress;
    }


    public static String secondToMinute(Integer second){
        StringBuilder sb =new StringBuilder();
        if(second == null || second == 0 ){
            return "0分";
        }
        Integer minute=second/60;
        Integer sec=second % 60;
        if(minute!=0){
            sb.append(minute).append("分");
        }
        if(sec!=0){
            sb.append(sec).append("秒");
        }
        return sb.toString();
    }

    public static String secondToDay(Object second){
        if(second == null){
            return "0分";
        }
        Long secondLong = Long.parseLong(second.toString());
        if(secondLong == null || secondLong == 0 ){
            return "0分";
        }
        long day    = secondLong / 86400;
        long hour   = secondLong % 86400 / 3600;
        long minite = secondLong % 86400 % 3600 / 60;
        long sec    = secondLong % 86400 % 3600 % 60;
        String timeStr = "";
        if (day > 0) {
            timeStr += day + "天";
        }
        if (hour > 0) {
            timeStr += hour + "小时";
        }
        if (minite > 0) {
            timeStr += minite + "分";
        }
        if (sec > 0) {
            timeStr += sec + "秒";
        }
        return timeStr;
    }



    /**
     * 格式化
     * 支持这种
     * 1998/10/10
     * 1998.10.10
     * 1998年10月10日
     * 2020-1-1
     * @param date
     * @return
     */
    public static String formate(String date){
        if(StringUtils.isBlank(date)){
            return "";
        }else {
            if("长期".equals(date)){
                return "2099-01-01";
            }
            date = date.split(" ")[0];
            date = date.replace(".","-");
            date = date.replace("/","-");
            date = date.replace("年","-");
            date = date.replace("月","-");
            date = date.replace("日","-");
            String[] dateArr = date.split("-");
            StringBuffer sb = new StringBuffer();
            for (int i = 0; i < dateArr.length; i++) {
                if(Integer.parseInt(dateArr[i])<10){
                    sb.append("0"+Integer.parseInt(dateArr[i]));
                }else {
                    sb.append(dateArr[i]);
                }
                if(i<2){
                    sb.append("-");
                }
            }
            try {
                LocalDate time = LocalDateTimeUtils.formatTime(sb.toString(),LocalDateTimeUtils.YYYYMMDD);
                return time.toString();
            }catch (Exception exception){
                exception.printStackTrace();
                throw new ServiceException(date+":日期格式错误");
            }
        }
    }


    public static String turnToChineseCharacter(String str){
        String[] arr = new String[]{"","一","二","三","四","五","六","七","八","九"};
        StringBuilder sb = new StringBuilder();
        int num = 0;
        for (int i = 0; i < str.length(); i++) {
            num = str.charAt(i) - '0';
            sb.append(arr[num]);
        }
        return sb.toString();
    }


    /**
     * 判断该对象是否所有属性为空
     * 返回ture表示所有属性为null，返回false表示不是所有属性都是null
     */
    public static boolean isAllFieldNull(Object object,String... fieldsName) {
        List<String> f = Arrays.asList(fieldsName);
        boolean flag = true;

        Class clazz = object.getClass();
        Field[] fields = clazz.getDeclaredFields();

        for (Field field : fields) {
            //设置属性是可以访问的(私有的也可以)
            field.setAccessible(true);
            if(f.indexOf(field.getName())>-1){
                continue;
            }
            Object value = null;
            try {
                value = field.get(object);
                if("".equals(value)){
                    value = null;
                }
                // 只要有1个属性不为空,那么就不是所有的属性值都为空
                if (value != null) {
                    flag = false;
                    break;
                }
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
        return flag;
    }

    public static LocalDate stringToLocalDate(String str) {
        if (StringUtils.isBlank(str)) {
            return null;
        }
        DateTimeFormatter dateTimeFormatter = null;
        if (localDate_pattern1.matcher(str).matches()) {
            dateTimeFormatter = DATE_TIME_FORMATTER1;
        } else if (localDate_pattern2.matcher(str).matches()) {
            dateTimeFormatter = DATE_TIME_FORMATTER2;
        } else {
            return null;
        }
        return LocalDate.parse(str, dateTimeFormatter);
    }

    /**
     * 根据经纬度获取位置
     * @param direction 方向 0-360
     * @return
     */
    public static String getDirection(Integer direction){
        if(direction == null){
            return "正北";
        }
        if(direction.equals(0) || direction.equals(360)){
            return "正北";
        }
        if(direction.equals(90)){
            return "正东";
        }
        if(direction == 180){
            return "正南";
        }
        if(direction == 270){
            return "正西";
        }
        if(direction>0&&direction<90){
            return "东北";
        }
        if(direction>90&&direction<180){
            return "东南";
        }
        if(direction>180&&direction<270){
            return "西南";
        }
        if(direction>270&&direction<360){
            return "西北";
        }
        return "";
    }



    public static void main(String[] args) {
        String html = "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 11px;\"><span style=\"font-family: 宋体;\">点滴小投入</span>&nbsp;●&nbsp;<span style=\"font-family: 宋体;\">爱车大保障</span></span><span style=\"font-family: Calibri; font-size: 9px;\">&nbsp;&nbsp;</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <strong><span style=\"font-family: Calibri; color: rgb(207, 19, 34); font-size: 9px;\"><span style=\"font-family: 宋体;\">合同号</span></span></strong><span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">：</span>&nbsp;${unix}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">客服电话：</span>400-778-7366</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri;\">&nbsp;</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap; text-align: center;\">\n" +
                "    <strong><span style=\"font-family: Calibri; font-size: 13px;\"><span style=\"font-family: 宋体;\">汽车延长质量保修服务合同</span></span></strong>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap; text-align: center;\">\n" +
                "    <strong><span style=\"font-family: Calibri; color: rgb(255, 169, 64); font-size: 11px;\"><span style=\"font-family: 宋体;\">基本信息</span></span></strong>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <strong><span style=\"font-family: Calibri; font-size: 11px;\"><span style=\"font-family: 宋体;\">客户信息</span></span></strong>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">个人客户姓名</span>/<span style=\"font-family: 宋体;\">企业客户名称：</span>${customName}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">通讯地址：</span>${formB}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">证件类型：身份证</span></span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-size: 9px; font-family: 宋体;\">证件号码：${customIdCard}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">联系电话：</span>${customPhone}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <strong><span style=\"font-family: Calibri; font-size: 11px;\"><span style=\"font-family: 宋体;\">车辆信息</span></span></strong>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">品牌：</span>${formE}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">车型：</span>${formG}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">排量：</span>${formH}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-size: 9px; font-family: 宋体;\">车辆识别号(VIN)：${vin}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-size: 9px; font-family: 宋体;\">发动机号：${engineNo}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">新车发票日期</span>/&nbsp;<span style=\"font-family: 宋体;\">机动车注册日期：</span>${formK}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">里程表数：</span>${formL}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <strong><span style=\"font-family: Calibri; font-size: 11px;\"><span style=\"font-family: 宋体;\">经销商信息</span></span></strong>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">经销商名称:<span style=\"font-family: Calibri; font-size: 9px; text-wrap-mode: wrap;\">${deptId}</span></span></span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">经销商地址：</span>${formP}</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <strong><span style=\"font-family: Calibri; font-size: 11px;\"><span style=\"font-family: 宋体;\">索克信息</span></span></strong>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">名称：重庆索克信息技术有限公司</span></span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">地址</span>&nbsp;<span style=\"font-family: 宋体;\">：</span>重庆市渝中区人民路13号第三层3-1-36</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">客服热线：</span><span style=\"font-family: Calibri; font-size: 9px; text-wrap-mode: wrap;\">400-778-7366</span></span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; text-wrap-mode: wrap;\">\n" +
                "    <strong><span style=\"font-family: Calibri; font-size: 11px;\"><span style=\"font-family: 宋体;\">产品信息</span></span></strong>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; white-space: normal;\">\n" +
                "    <span style=\"font-size: 9px;\"><span style=\"font-family: 宋体;\">产品名称：</span></span><span style=\"font-size: 9px; font-family: 宋体;\">0516全国尊享11大系统</span>\n" +
                "</p>\n" +
                "<p style=\"margin-top: 4px; margin-bottom: 4px; white-space: normal;\">\n" +
                "    <span style=\"font-family: Calibri; font-size: 9px;\"><span style=\"font-family: 宋体;\">延保期限：延长保修至5年/16万公里(从新车购车日期起算，原厂质保年限结束后开始生效)</span></span>\n" +
                "</p>\n";


    }

    public static String getExtended(String input){
        List<String> matches = new ArrayList<>();
        String regex = "\\d+年/\\d+万公里"; // 正则表达式匹配正整数年/正整数万公里
        Pattern pattern = Pattern.compile(regex);
        Matcher matcher = pattern.matcher(input);
        while (matcher.find()) {
            // 对于每次匹配，将匹配的子字符串添加到列表中
            matches.add(matcher.group());
            return "延长保修至"+matcher.group()+"(从新车购车日期起算)";
        }
        return "";

    }

    // 方法1：使用正则表达式直接匹配
    public static String extractProductNameWithRegex(String html) {
        Pattern pattern = Pattern.compile("产品名称：([^<]+)");
        Matcher matcher = pattern.matcher(html);
        if (matcher.find()) {
            return matcher.group(1).trim();
        }
        return null;
    }
}
